from dataclasses import dataclass
from typing import Optional, final
from server.apps.main.logic.todo.queries import get_description, get_title


@final
@dataclass
class ValidTodoData(object):
    """Used to represent valid data that we can use to create a ."""

    title: str
    description: str
    is_done: bool


def validate(
    title: Optional[str],
    description: Optional[str],
    is_done: Optional[bool]
) -> Optional[ValidTodoData]:
    """Validates incomming data and decides if we can save it as a post."""
    if not title or not description:
        return None
    return ValidTodoData(is_done=is_done, title=title, description=description)


def validate_edit(id: int,
    title: Optional[str],
    description: Optional[str],
    is_done:Optional[bool]) -> Optional[ValidTodoData]:
    if not title:
        title = get_title(id)
    if not description:
        description = get_description(id)
    return ValidTodoData(title=title, description=description,is_done=is_done)